class Solution {
public:
    ListNode* detectCycle(ListNode* head) {
        ListNode* slow = head;
        ListNode* fast = head;
        while (fast && fast->next)
        {
            slow = slow->next;
            fast = fast->next->next;
            if (slow == fast)
            {
                ListNode* meet = slow;
                while (meet != head)
                {
                    meet = meet->next;
                    head = head->next;
                }
                return meet;
            }
        }
        return nullptr;
    }
};